package my.engine.client.ws.login;

import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 *
 * @author Администратор
 */
public class LoginServiceImpl {

    private static LoginServiceImpl instance;
    private static MessageDigest digest;

    static {
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static LoginServiceImpl getInstance() {
        if (instance == null) {
            instance = new LoginServiceImpl();
        }
        return instance;
    }

    public boolean sayHello(String login, String passwd) {
        try {
            digest.update(login.getBytes("UTF-8"));
            digest.update(passwd.getBytes("UTF-8"));
            digest.update(digest.digest());
            byte[] result = digest.digest();

            System.setProperty("javax.net.ssl.trustStore", "C:\\Documents and Settings\\EMikolenko\\.keystore");
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

            HttpsURLConnection.setDefaultHostnameVerifier(
                    new HostnameVerifier() {
                        public boolean verify(String arg0, SSLSession arg1) {
                            return true;
                        }
                    });
            Authenticator.setDefault(new MyAuthenticator(login, passwd));

            LoginServiceService service = new LoginServiceService();
            return service.getLoginServicePort().sayHello(login, byte2hex(result));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(LoginServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;

//        HelloService service = new HelloService();
//Hello proxy = (service.getHelloPort());
//((BindingProvider)proxy).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "userfoo");
//((BindingProvider)proxy).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "passbar"); 
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    private class MyAuthenticator extends Authenticator {

        private String login;
        private String passwd;

        public MyAuthenticator(String login, String passwd) {
            this.login = login;
            this.passwd = passwd;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(login, passwd.toCharArray());
        }
    }
}
